﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Data.Linq;
using TaobaoPick.util;

namespace TaobaoPick.storage
{
    public class ImageSpool
    {
        protected static String rootPath = "D:\\taobaopick\\Images\\";

        protected String pickExtension(String imgURL)
        {
            if (String.IsNullOrWhiteSpace(imgURL))
                return null;
            int pos = imgURL.LastIndexOf(".");
            if (pos > 0)
            {
                return imgURL.Substring(pos);
            }
            return null;
        }

        public Guid preloadItemFromCache(String imgUrl)
        {
            MyTaobao1DataContext context = LinqAgent.getTaobaoContext();
            ImageStorage img = (from image in context.ImageStorages where image.imageSrc == imgUrl select image).SingleOrDefault();
            if (img == null)
            {
                String localfile = TaobaoPick.cache.IECache.findCacheEntry(imgUrl);
                Guid itemId=createImageItem(imgUrl, localfile);
                return itemId;                
            }
            else
            {
                //已經有項目了 ,先看有沒有local
                if (checkLocalPath(img.localPath) == false)
                {
                    //沒有local, 那就需要copy cache
                    String localfile = TaobaoPick.cache.IECache.findCacheEntry(imgUrl);

                    if (localfile != null)
                    {
                        File.Copy(localfile, rootPath + img.localPath);
                        img.exist = true;
                        img.localSaved = true;
                        context.SubmitChanges();
                    }
                    else
                    {
                        if (img.localSaved == true)
                        {
                            //沒有local 也沒有cache
                            //如果本來有,後來沒有,那就需要更新資料庫
                            img.localSaved = false;
                            context.SubmitChanges();
                        }
                    }
                }
                   //拷貝
                return img.uid;
            }
        }
        /********************************************************************
         * 
         * *******************************************************************/
        public ImageSpool()
        {
            System.IO.Directory.CreateDirectory(rootPath);
        }

        protected Boolean checkLocalPath(String relPath)
        {
            FileInfo fi = new FileInfo(rootPath + relPath);
            return fi.Exists;               
        }
        /************************************************************************************************************
         * 
         * 
         * 
         * **********************************************************************************************************/
        public Boolean fileExists(String fileName)
        {
            if ("".Equals(fileName))
                return false;
            System.IO.FileInfo f = new System.IO.FileInfo(fileName);
            return f.Exists;
        }
        /************************************************************************************************************
         * 
         * 
         * 
         * **********************************************************************************************************/
        public Guid createImageItem(String srcURL, String localPath)
        {
            if(localPath==null)
                localPath="";

            String extension = pickExtension(srcURL);
            MyTaobao1DataContext context=LinqAgent.getTaobaoContext();

            ImageStorage imgStore = (from i in context.ImageStorages where i.imageSrc == srcURL select i).SingleOrDefault();
            if (imgStore != null)
            {
                   String subpath=imgStore.localPath;
                    if(checkLocalPath(subpath)==false)
                    {
                        if (fileExists(localPath))
                        {
                            //拷貝.
                            File.Copy(localPath, rootPath + subpath);
                            imgStore.exist=true;
                            imgStore.localSaved=true;
                            context.ImageStorages.Attach(imgStore);
                            context.SubmitChanges();
                        }
                    }
                return imgStore.uid;
            }
            else //新項目
            {
                Guid id = NewGUID.create("imgs");
                String subp = DateTime.Now.ToString("yyyy-MM-dd");
                Directory.CreateDirectory(rootPath+subp);
                String relpath = subp + "\\" + id.ToString() + extension;
                imgStore = new ImageStorage();
                imgStore.uid = id;
                 imgStore.imageSrc = srcURL;
                 imgStore.localPath = relpath;
                imgStore.localSaved=false;
                imgStore.exist=false;
                //看看是不是有突
                  if (fileExists(localPath))
                   {
                     File.Copy(localPath, rootPath + relpath);
                    imgStore.localSaved=true;
                    imgStore.exist=true;
                   }
                context.ImageStorages.InsertOnSubmit(imgStore);
                context.SubmitChanges();
                return id;
            }
        }

        public String getImagePath(Guid imageId)
        {

            MyTaobao1DataContext context = LinqAgent.getTaobaoContext();
            ImageStorage imgStore = (from i in context.ImageStorages where i.uid== imageId select i).SingleOrDefault();
            if (imgStore != null)
            {
                String fileName = rootPath + imgStore.localPath;
                FileInfo fi = new FileInfo(fileName);
                if (fi.Exists)
                    return fileName;
            }
            return null;
        }
    }
}
